/**
 * creater:chuanpeng.zhu
 * create time:2015/12/30
 * describe：本地存储服务
 */
APP
/**
 * 持久化存储
 */
  .factory('$localstorage', ['$window', '$rootScope', function ($window, $rootScope) {
    return {
      set: function (key, value) {
        try {
          $window.localStorage[key] = value;
        } catch (e) {
          $rootScope.$broadcast('LOCALSTORAGE_FULL');
        }
      },
      get: function (key, defaultValue) {
        return $window.localStorage[key] || defaultValue;
      },
      setObject: function (key, value) {
        try {
          $window.localStorage[key] = JSON.stringify(value);
        } catch (e) {
          $rootScope.$broadcast('LOCALSTORAGE_FULL');
        }
      },
      getObject: function (key) {
        return JSON.parse($window.localStorage[key] || '{}');
      },
      setArray: function (key, value) {
        try {
          $window.localStorage[key] = JSON.stringify(value);
        } catch (e) {
          $rootScope.$broadcast('LOCALSTORAGE_FULL');
        }
      },
      getArray: function (key) {
        return JSON.parse($window.localStorage[key] || '[]');
      },
      size: function () {
        return $window.localStorage.length;
      },
      clear: function () {
        return $window.localStorage.clear();
      }
    }
  }])

/**
 * describe：缓存服务
 */
  .service('CacheService', ['$window', '$rootScope', function ($window, $rootScope) {
    return {
      set: function (key, value) {
        $window.sessionStorage[key] = value;
      },
      get: function (key, defaultValue) {
        return $window.sessionStorage[key] || defaultValue;
      },
      setObject: function (key, value) {
        $window.sessionStorage[key] = JSON.stringify(value);
      },
      getObject: function (key) {
        return JSON.parse($window.sessionStorage[key] || '{}');
      },
      setArray: function (key, value) {
        $window.sessionStorage[key] = JSON.stringify(value);
      },
      getArray: function (key) {
        return JSON.parse($window.sessionStorage[key] || '[]');
      },
      size: function () {
        return $window.sessionStorage.length;
      },
      clear: function () {
        return $window.sessionStorage.clear();
      }
    }
  }])
;
